package com.fanbai.score.user.fragemnt;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.activity.ElegantExchange_Activity;
import com.fanbai.score.user.activity.ShopDetails_Activity;
import com.fanbai.score.user.bean.ProductInfo_Bean;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.fanbai.score.user.http.UserProduct_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.media.CamcorderProfile.get;

/**
 * Created by acer on 2017/5/13.
 * 贵人兑换
 */

public class Home_Exchange_Fragment extends BaseFragment{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Home_Exchange_Fragment newInstance(int sectionNumber) {
        Home_Exchange_Fragment fragment = new Home_Exchange_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Home_Exchange_Fragment() {
    }

    @Override
    protected int getLayoutID() {
        return R.layout.user_fragment_home_red;
    }

    @Override
    public String getTitleName() {
        return null;
    }

    @Override
    public void iniTitleLeft(ImageView tv) {

    }

    @Override
    public void iniTitleRight(TextView tv) {

    }

    @Override
    protected void initView(View view) {
        mRecyclerView.addItemDecoration(new DividerItemDecoration(ctx,
                DividerItemDecoration.VERTICAL));
    }
    @Override
    protected void initData() {
        requestShopClass();

    }

    @Override
    protected void initListener() {

    }

    List<ProductInfo_Bean> beans;
    private void setClassListData(List<ProductInfo_Bean> listBeans){
        if(beans == null){
            beans = new ArrayList<>();
        }else{
            beans.clear();
        }
        beans.addAll(listBeans);
//        mDatas = new ArrayList<>();
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494156320411&di=ee09c92ad8fa24ca4fe0efb54d902133&imgtype=0&src=http%3A%2F%2Fimg.newmotor.com.cn%2FUploadFiles%2Fimage%2F20160506%2F20160506114832563256.png");
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
        ProductInfo_Bean bean = new ProductInfo_Bean();
        bean.setPid("-1");
        bean.setPimages(null);
        beans.add(0, bean);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
//        mRecyclerView.setNestedScrollingEnabled(false);
        CommonAdapter adapter = new CommonAdapter<ProductInfo_Bean>(ctx, R.layout.user_item_fragment_goods,
                beans){
            @Override
            protected void convert(ViewHolder holder, ProductInfo_Bean bean, int position) {
                if(bean.getPimages() != null && bean.getPimages().size() > 0){
                    ImageUtils.show(ctx, bean.getPimages().get(0), (ImageView) holder.getView(R.id.userItemFragment_goodsImg), R.mipmap.icon_img_defeated);
                }else{
                    holder.setImageResource(R.id.userItemFragment_goodsImg, R.mipmap.icon_img_defeated);
                }
//                holder.setText(R.id.userItemFragment_goodsTitle, bean.getPname());
//                holder.setText(R.id.userItemFragment_goodsPrice, bean.getPprice());
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(position == 0){
                    ctx.jumpToActivity(ElegantExchange_Activity.class);
                }else{
                    ProductInfo_Bean bean = beans.get(position);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("bean", bean);
                    ctx.jumpToActivity(ShopDetails_Activity.class, bundle);
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    private void requestShopClass(){
        ctx.showDialog();
        UserProduct_RequestNet_Utils.getProductInfos(ctx, new CallBack<List<ProductInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ProductInfo_Bean> beans) {
                ctx.hideDialog();
                setClassListData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
